package com.gonsin.gview.example.bean;

import com.gonsin.gview.annotations.GView;
import com.gonsin.gview.annotations.board.GBoard;
import com.gonsin.gview.annotations.board.GButton;
import com.gonsin.gview.annotations.data.GData;
import com.gonsin.gview.annotations.data.GListData;
import com.gonsin.gview.annotations.data.GTableData;
import com.gonsin.gview.annotations.data.GTreeData;
import com.gonsin.gview.annotations.board.form.*;
import com.gonsin.gview.annotations.board.table.*;
import com.gonsin.gview.annotations.enums.ButtonAction;
import com.gonsin.gview.annotations.enums.ColumnType;
import com.gonsin.gview.example.data.GroupDataPicker;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.persistence.*;

@GView(
        viewName = "GroupBean",
        title = "用户组",
        boards = @GBoard(
                table = @GTable(
                        header = @GHeader(
                                buttons = {
                                        @GButton(name = "添加用户组", action = ButtonAction.VIEW_BY_DIALOG, view = "AddGroupBean"),
                                },
                                searchBar = @GSearchBar(filters = {
                                        @GSearchFilter(textInput = @GTextInput(name = "组名字", keyName = "name")),
//                                        @GSearchFilter(
//                                                selector = @GSelector(
//                                                        name = "年份",
//                                                        keyName = "year",
//                                                        listData = @GListData(
//                                                                keyName = "年份",
//                                                                valueName = "year",
//                                                                fromSql = "select distinct from_unixtime( `start` / 1000, '%Y' ) as 'year' from `gs_room_order`"
//                                                        )
//                                                )
//                                        )
                                })
                        ),
                        tableData = @GTableData(fromTableDataPicker = GroupDataPicker.class, keyName = "groupId"),
                        itemName = "item",
                        columns = {
//                                @GColumn(columnType = ColumnType.SELECTOR, keyName = "groupId"),
                                @GColumn(name = "用户组名字", keyName = "name", columnType = ColumnType.TEXT),

                                // TODO 枚举类型的列
                                @GColumn(name = "审核权", keyName = "audit", columnType = ColumnType.ENUMS, enums = {@GEnum(value = "true", text = "是"), @GEnum(value = "false", text = "否")}),
                                @GColumn(name = "免审核权", keyName = "freeAudit", columnType = ColumnType.ENUMS, enums = {@GEnum(value = "true", text = "是"), @GEnum(value = "false", text = "否")}),
                                @GColumn(name = "管理员", keyName = "admin", columnType = ColumnType.ENUMS, enums = {@GEnum(value = "true", text = "是"), @GEnum(value = "false", text = "否")}),
                                @GColumn(name = "后勤", keyName = "waiter", columnType = ColumnType.ENUMS, enums = {@GEnum(value = "true", text = "是"), @GEnum(value = "false", text = "否")}),
                                @GColumn(name = "操作", columnType = ColumnType.BUTTONS, buttons = {
                                        @GButton(name = "修改", action = ButtonAction.VIEW_BY_DIALOG, view = "UpdateGroupBean", props = @GProp(keyName = "groupId", valueName = "groupId")),
                                        @GButton(name = "删除", action = ButtonAction.CONFIRM, confirmMessage = "是否要删除【${item.name}】?", actionName = "deleteGroup", props = @GProp(keyName = "groupId", valueName = "groupId"))
                                }),
                        }
                )
        )
)

@GView(
        viewName = "AddGroupBean",
        title = "添加用户组",
        boards = @GBoard(
                form = @GForm(
                        formItems = {
                                @GFormItem(textInput = @GTextInput(name = "组名称", keyName = "name")),
                                @GFormItem(checkBox = @GCheckBox(name = "审核权", keyName = "audit")),
                                @GFormItem(checkBox = @GCheckBox(name = "免审核权", keyName = "freeAudit")),
                                @GFormItem(checkBox = @GCheckBox(name = "管理员", keyName = "admin")),
                                @GFormItem(checkBox = @GCheckBox(name = "后勤", keyName = "waiter")),
                                @GFormItem(treeCheckBox = @GTreeCheckBox(
                                        name = "权限",
                                        keyName = "permissions",
                                        treeData = @GTreeData(fromListDataPicker = GroupDataPicker.class, valueName = "value", keyName = "name", parentKeyName = "parent"))
                                )
                        },
                        okButton = @GButton(name = "确认", action = ButtonAction.CONFIRM_AND_CLOSE, actionName = "addGroup"),
                        cancelButton = @GButton(name = "取消", action = ButtonAction.CLOSE_DIALOG)
                )
        )
)

@GView(
        viewName = "UpdateGroupBean",
        title = "修改用户组",
        props = "groupId",
        boards = @GBoard(
                form = @GForm(
                        data = @GData(fromSql = "select " +
                                "g.id as groupId," +
                                "g.name," +
                                "g.audit," +
                                "g.free_audit as freeAudit," +
                                "g.admin," +
                                "g.waiter," +
                                "g.permissions" +
                                "from gs_user_group where g.id = ${groupId}"),
                        formItems = {
                                @GFormItem(textInput = @GTextInput(name = "组名称", keyName = "name")),
                                @GFormItem(checkBox = @GCheckBox(name = "审核权", keyName = "audit")),
                                @GFormItem(checkBox = @GCheckBox(name = "免审核权", keyName = "freeAudit")),
                                @GFormItem(checkBox = @GCheckBox(name = "管理员", keyName = "admin")),
                                @GFormItem(checkBox = @GCheckBox(name = "后勤", keyName = "waiter")),
                                @GFormItem(treeCheckBox = @GTreeCheckBox(
                                        name = "权限",
                                        keyName = "permissions",
                                        treeData = @GTreeData(fromListDataPicker = GroupDataPicker.class, keyName = "name", parentKeyName = "parent"))
                                )
                        },
                        okButton = @GButton(name = "确认", action = ButtonAction.CONFIRM_AND_CLOSE, actionName = "updateGroup"),
                        cancelButton = @GButton(name = "取消", action = ButtonAction.CLOSE_DIALOG)
                )
        )
)

/**
 * 角色表
 */
@Data
@ApiModel("角色表")
@Entity
@Table(name = "temp_gs_user_group")
public class GroupBean  {
    /**
     *  @GeneratedValue(strategy = GenerationType.IDENTITY) 单表自增
     *  @GeneratedValue() 默认自增是多表同时自增
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @ApiModelProperty("主键")
    @Column(name = "id", columnDefinition = "int comment '主键'")
    private int groupId;

    @ApiModelProperty("角色名字")
    @Column(name = "name", columnDefinition = "varchar(64) comment '角色名字'")
    private String name;       // 角色名字

    @ApiModelProperty("角色类型 1最高权限 2次级")
    @Column(name = "type", columnDefinition = "int comment '角色类型 1最高权限 2次级'")
    private Byte type;       //角色类型 1最高权限 2次级 3普通

    @ApiModelProperty("会议大厦id")
    @Column(name = "edifice_id", columnDefinition = "int comment '会议大厦id'")
    private int edificeId;

    @ApiModelProperty("用户组权限")
//    @Column(name = "gs_permissions")
    @Column(name="gs_permissions", columnDefinition="TEXT comment '用户组权限'", nullable = true)   // required
    private String permissions;

    @ApiModelProperty("是否有审核权")
    @Column(name = "audit", columnDefinition = "tinyint comment '是否有审核权'")
    private boolean audit;

    @ApiModelProperty("是否有免审核权")
    @Column(name = "free_audit", columnDefinition = "tinyint comment '是否有审核权'")
    private Boolean freeAudit;

    @ApiModelProperty("是否为管理员（用于接受反馈信息）")
    @Column(name = "gs_admin", columnDefinition = "tinyint comment '是否为管理员'")
    private Boolean admin;

    @ApiModelProperty("是否为后勤(用于接受呼叫服务)")
    @Column(name = "gs_waiter", columnDefinition = "tinyint comment '是否为后勤'")
    private Boolean waiter;
}
